<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        div{
            width:300px;
            height: 46px;
            background-color: brown;
            margin:20px auto 100px auto;
            border-radius: 20px;
            text-align: center;
        }
        div a{
             color:white;
             text-decoration: none;
             font-size: 20px;
             line-height: 46px;
        }
        h1{
            width:200px;
            margin:20px auto;
        }
    </style>
</head>
<body>
    <h1>学生信息表</h1>
    <table border="1" cellspacing="0" cellpadding="10" align="center">
        <thead>
            <tr>
                <th>编号</th>
                <th>姓名</th>
                <th>联系方式</th>
                <th>年龄</th>
                <th>性别</th>
                <th>邮箱</th>
                <th>备注</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>

        </tbody>
    </table>
    <div>
        <a href="add.html">添加学生信息</a>
    </div>
    <script src="./jquery.min.js"></script>
    <script>
        //ajax传参数
        $.get('/users').done(function(result){
            console.log(result);
            //遍历输出的数据
            $(result).map(function(index,value){
                var row = '';
                //讲增加的内容放入到html中去
                row += '<tr><td>'+value.id+
                    '</td><td>' + value.username +
                    '</td><td>' + value.phone +
                    '</td><td>' + value.age+
                    '</td><td>'+value.gender+
                    '</td><td>'+value.email+
                    '</td><td>'+value.desc+'</td><td><button onclick="del('+value.id+
                    ')">删除</button><a href="./edit.html#'+value.id+
                    '">编辑</a></td></tr>';
                $('tbody').append(row);
               
            });
           
        });
        //删除操做
        function del(id){
            //判断用户是否确定删除数据
            if(confirm('确定删除该数据吗？你真的真的确定吗？')==true){
                console.log(id);
                //Ajax传参，删除后重新加载页面
                $.get('/users/del', { id: id }).done(function (res) {
                    //重新加载该页面的两种方法
                    //location.href = './1.html';
                    location.reload();
                });
            }
        }
        
    </script>
</body>
</html>